
function initialize(latitude, longitude) {
	var myLatlng = new google.maps.LatLng(latitude, longitude);
	var mapOptions = {
		zoom : 15,
		center : myLatlng,
		mapTypeId : google.maps.MapTypeId.ROADMAP
	}
	var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

	var marker = new google.maps.Marker({
		position : myLatlng,
		map : map,
		title : 'Hello World!',
		draggable : true
	});
	google.maps.event.addListener(marker, 'dragend', function(event) {
		var position = event.latLng;
		updateContactLatLng(position.lat(),position.lng());
		//alert('Latitude: ' position.lat() + '\n\rLongitude: ' +position.lng());
	});
}

function updateContactLatLng(latitude, longitude) {

	var url = "/rest/contact/update";
	var params = 'latitude' + '=' + latitude+'&longitude='+longitude;

	Adminexecute.execute(url, params, function() {
	});
}

function updateContact(node) {

	var fieldName;
	var fieldValue

	fieldName = $(node).attr('name');
	fieldValue = encodeURIComponent(node.innerHTML);

	var url = "/rest/contact/update";
	var params = fieldName + '=' + fieldValue;

	Adminexecute.execute(url, params, function() {
	});
}

var url = "/rest/contact/get_contact";
var params = null;

function putContactValues(node,contactValues){
	var childNodes= $(node).find('*[name]');
	var childNode;
	var name;
	for(var i =0; i<childNodes.length;i++){
		childNode = childNodes[i];		
		name = $(childNode).attr('name');
		if(contactValues[name]){
			$(childNode).html(contactValues[name]);
		}				
	}
}

Adminexecute.retrieve(url, params, function(responseText) {
	var contact = eval('('+responseText+')');
	var node = document.getElementsByClassName('contact_info')[0];
	putContactValues(node,contact);
	if(contact.latitude && contact.longitude){
		google.maps.event.addDomListener(window, 'load', initialize(contact.latitude, contact.longitude));
	}else{
		google.maps.event.addDomListener(window, 'load', initialize(77, 106));
	}
	
});